package zzz.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import zzz.core.Constants;
import zzz.model.User;
import zzz.service.UserService;

@Controller
@RequestMapping("/user")
public class UserAction extends BaseAction{
	
	@Autowired(required=true)
	private UserService userService; 
	
	@RequestMapping("/login")
	public String login(User user,HttpSession session,Map<String,String> m){
		User u=userService.login(user);
		String result=null;
		if(u!=null){
			if(u.getUserPwd().equals(user.getUserPwd())){
				session.setAttribute(Constants.LOGIN_USER, u);
				//System.out.println("user"+session.getAttribute(Constants.LOGIN_USER));
				m.put("result","yes");
				result="redirect:../index";
			}else{
				m.put("msg", "用户密码错误");
				m.put("herf", "loginpage");
				result="common/msgModal";
			}
		}else{
			m.put("msg", "用户不存在");
			m.put("herf", "loginpage");
			result="common/msgModal";
		}
		return result;
	}
	
	//退出登录
	@RequestMapping("/logout")
	public String loginOut(){
		getSession().invalidate();
		return "redirect:../index";
	}
	@RequestMapping("/loginpage")
	public String loginPage(Map<String,List> m){
		m=new HashMap<String,List>();
		return "login";
	}
	@RequestMapping("/registpage")
	public String registPage(Map<String,List> m){
		m=new HashMap<String,List>();
		return "common/regist";
	}
	
	//注册
	@RequestMapping("/regist")
	public String getIndexPage(User user){
		//m=new HashMap<String,List>();
		boolean f=userService.regUser(user);
		System.out.println(user);
		if(f){
			return "login";
		}else{
			return "common/regist";
		}
	}
	
	

}
